﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HsTec.DigitalPlatform.DataAccess
{
    public class ClassInfoRepository
    {
        DigitalPlatformEntities db = new DigitalPlatformEntities();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="classinfo"></param>
        public void Add(ClassInfo classinfo)
        {
            db.AddToClassInfos(classinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="classinfo"></param>
        public void Delete(ClassInfo classinfo)
        {
            db.DeleteObject(classinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            db.SaveChanges();
        }

        /// <summary>
        /// 根据ID查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ClassInfo GetClassInfoByID(int id)
        {
            return db.ClassInfos.FirstOrDefault<ClassInfo>(d => d.classid == id);
        }

        /// <summary>
        /// 返回所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<ClassInfo> FindALLClassInfo()
        {
            return db.ClassInfos;
        }

        public IQueryable<ClassInfo> PageInfo(int page, int rp, string sortname, string sortorder)
        {
            return db.ClassInfos.OrderBy("it." + sortname + " " + sortorder).Skip((page - 1) * rp).Take(rp);
        }

        public int GetAllCount()
        {
            return db.ClassInfos.Count();
        }

        public IQueryable FindClassInfoBySpecID(int specid, int gradeid)
        {
            return from c in db.ClassInfos
                   where c.specid == specid && c.classno == gradeid
                   select new
                   {
                       id = c.classid,
                       name = c.classname
                   };
        }
    }
}
